﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlashCards.WebApp.DataAccessLayer;
using FlashCards.WebApp.Models;
using FlashCards.WebApp.Utils;

namespace FlashCards.WebApp.Controllers
{
    [Authorize]
    public class FriendsController : Controller
    {
        //
        // GET: /Friends/
        public ActionResult Index()
        {
            var friends = FriendshipUtils.GetUserFriends(User.Identity.Name);
            var invitations = FriendshipUtils.GetInvitationsReceived(User.Identity.Name);

            var model = new FriendshipsModel(friends, invitations, null);

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            string searchPattern = collection["SearchPattern"];

            var friends = FriendshipUtils.GetUserFriends(User.Identity.Name);
            var invitations = FriendshipUtils.GetInvitationsReceived(User.Identity.Name);
            var searched = FriendshipUtils.GetUnknownUsers(searchPattern, User.Identity.Name);

            var model = new FriendshipsModel(friends, invitations, searched);

            return View(model);
        }

        [HttpPost]
        public ActionResult SendFriendshipInvitation(int? friendId)
        {
            if (friendId.HasValue)
            {
                try
                {
                    FriendshipUtils.AddInvitation(User.Identity.Name, friendId.Value);
                    return View();
                }
                catch (Exception ex)
                {
                    //TO-DO - logowanie bledow

                    ModelState.AddModelError("", "Sending invitation failed");
                }
            }
            else
                ModelState.AddModelError("", "Friend name cannot be empty");

            return View();
        }

        [HttpPost]
        public ActionResult ConfirmFriendshipInvitation(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                FriendshipUtils.ConfirmFriendship(User.Identity.Name, userName);
            }

            return RedirectToAction("Index", "Friends");
        }


        public ActionResult DeleteFriendship(string friendNick)
        {
            if (!string.IsNullOrEmpty(friendNick))
            {
                var friend = FriendshipUtils.GetUserFriends(User.Identity.Name).SingleOrDefault(x => x.Nick == friendNick);
                if (friend != null)
                {
                    var model = new FriendModel(friend);
                    return View(model);
                }
                
            }
            else
                ModelState.AddModelError("", "Friend name is empty");
            return View();
            
        }

        [HttpPost]
        public ActionResult DeleteFriendship(string friendNick, int fakeParam)
        {
            if (!string.IsNullOrEmpty(friendNick))
            {
                FriendshipUtils.DeleteFriendship(User.Identity.Name, friendNick);
            }
            return RedirectToAction("Index", "Friends");
        }

        public ActionResult GetUserAvatar(int userId)
        {
            var imageData = UserUtils.GetUserAvatarData(userId);
            return File(imageData, "image/jpg");
        }
    }
}
